package com.example.test.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.FeatureDescriptor;
import java.util.Optional;
import java.util.stream.Stream;

public class ProjectUtil {

    /**
     * 将对象A中与B中的同字段的值赋值到B中
     * @param A
     * @param B
     * @return
     */
    public static Object aCopyB(Object A, Object B){
        Optional<Object> r = Optional.of(B);
        if(!r.isPresent()){
            return null;
        }
        String [] dataS = getProjectData(A);
        BeanUtils.copyProperties(A, r.get(), dataS);
        return r.get();
    }

    /**
     * 获取对象属性
     * @return
     */
    public static String[] getProjectData(Object object){
        final BeanWrapper beanWrapper = new BeanWrapperImpl(object);
        return Stream.of(beanWrapper.getPropertyDescriptors())
                .map(FeatureDescriptor::getName)
                .filter(propertyName -> beanWrapper.getPropertyValue(propertyName) == null)
                .toArray(String[]::new);
    }



}
